package com.example.game;

import java.util.Comparator;
import java.util.PriorityQueue;

import android.graphics.Point;

/*
 * this class for analyze all coins in board 
 * player one is master player  how could eat
 */
public class Analyzer {
	Comparator<Move> comparator = new MoveComparator();
	PriorityQueue<Move> decision = new PriorityQueue<Move>(24, comparator);
	Comparator<Point> Eatcomparator = new EatComparator();
	PriorityQueue<Point> eat = new PriorityQueue<Point>(24, Eatcomparator);
	Comparator<Point> pushcomparator = new EatComparator();
	PriorityQueue<Point> push = new PriorityQueue<Point>(24, Eatcomparator);
	private final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
	public int[] palyerOneCoins;
	public int[] palyerTwoCoins;
	public int[] cell = new int[24];
	public Player playerOne, playerTwo;
	private int[][] neighbours;

	public Analyzer(Player playerOne, Player playerTwo) {
		this.playerOne = playerOne;
		this.playerTwo = playerTwo;
		neighbours = playerOne.getNeighbours();
		palyerOneCoins = playerOne.getCoins();
		palyerTwoCoins = playerTwo.getCoins();
		for (int i = 0; i < 24; i++)
			cell[i] = 0;
		for (int i = 0; i < 9; i++) {
			if (palyerOneCoins[i] >= 0 && palyerOneCoins[i] <= 23)
				cell[palyerOneCoins[i]] = 1;
			if (palyerTwoCoins[i] >= 0 && palyerTwoCoins[i] <= 23)
				cell[palyerTwoCoins[i]] = 2;
		}
	}

	public Boolean CouldEatAfterMove(int cellId, int playerId) {
		Boolean result = false;
		if (isEmptyCell(cellId)) {
			cell[cellId] = playerId;
			result = inThree(cellId);
			cell[cellId] = 0;
		}
		return result;
	}

	int Move(int cellId, int direction) {
		if (neighbours[cellId][direction] == cellId)
			return 0;
		else
			return neighbours[cellId][direction];
	}

	public Boolean inThreeDirection(int cellId, int direction) {
		int counter = 1;
		int next = cellId;
		while (true) {
			next = Move(next, direction);
			if (next == 0)
				break;
			if (cell[next] == cell[cellId])
				counter++;
		}
		return (counter >= 3);
	}

	public Boolean inThree(int cellId) {
		for (int i = 0; i < 4; i++)
			if (inThreeDirection(cellId, i))
				return true;
		return false;
	}

	public Boolean isEmptyCell(int cellId) {
		return (cell[cellId] == 0);
	}

	public void getAllMovies(int playerId) {
		for (int i = 0; i < 24; i++) {
			if (cell[i] == playerId) {
				for (int j = 0; j < 4; j++) {
					if (neighbours[i][j] != i && isEmptyCell(neighbours[i][j])) {
						int p;
						if (CouldEatAfterMove(neighbours[i][j], playerId)) {
							p = 5;
						} else if (CouldEatAfterMove(neighbours[i][j],
								getOtherPlayerId(playerId))) {
							p = 4;
						} else {
							p = 3;
						}
						decision.add(new Move(i, neighbours[i][j], p));
					}
				}
			}
		}
	}

	public int getOtherPlayerId(int playerId) {
		if (playerId == 1)
			return 2;
		if (playerId == 2)
			return 1;
		return 0;
	}

	public void iniDecisions(int playerId) {
		decision.clear();
		getAllMovies(playerId);
	}

	void iniEat(int playerId) {
		playerId = this.getOtherPlayerId(playerId);
		for (int i = 0; i < 24; i++) {
			if (cell[i] == playerId && !inThree(i)) {
				for (int j = 0; j < 4; j++) {
					if (neighbours[i][j] != i && isEmptyCell(neighbours[i][j])) {
						int p;
						if (CouldEatAfterMove(neighbours[i][j], playerId)) {
							p = 5;
						} else {
							p = 3;
						}
						eat.add(new Point(i, p));
					}
				}
			}
		}
	}

	void iniPush(int playerId) {
		for (int i = 0; i < 24; i++) {
			if (isEmptyCell(i)) {
				int p;
				if (CouldEatAfterMove(i, playerId)) {
					p = 5;
				} else {
					p = 3;
				}
				push.add(new Point(i, p));
			}
		}
	}
}
